package com.atguigu.tingshu.user.service.strategy.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.atguigu.tingshu.user.service.strategy.PaidRecordStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * ClassName: VIPPaidRecordStrategy
 * Package: com.atguigu.tingshu.user.service.strategy
 * Description:
 *
 * @Author 咚咚小圆帽
 * @Create 2025/8/4 Monday 17:34
 * @Version 1.0
 */
@Slf4j
//指定Bean对象的id，跟发货时货物的类型是一致的
@Component("1003")
public class VIPPaidRecordStrategy implements PaidRecordStrategy {

    @Autowired
    private UserVipServiceMapper userVipServiceMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private VipServiceConfigMapper vipServiceConfigMapper;

    /**
     * 处理会员的发货逻辑
     * @param userPaidRecordVo
     */
    @Override
    public void handlerPaidRecord(UserPaidRecordVo userPaidRecordVo) {
        log.info("[用户服务]处理会员发货逻辑");
        //3.处理虚拟物品发货-VIP会员
        //3.0 根据订单编号查询已购声音表判断是否处理过该笔订单
        LambdaQueryWrapper<UserVipService> userVipServiceLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userVipServiceLambdaQueryWrapper.eq(UserVipService::getOrderNo, userPaidRecordVo.getOrderNo());
        Long count = userVipServiceMapper.selectCount(userVipServiceLambdaQueryWrapper);
        if (count > 0) {
            return;
        }

        //3.1 新增会员购买记录
        //3.1.1 初始化已购会员对象
        UserVipService userVipService = new UserVipService();
        //3.1.2 为订单编号、用户id赋值
        userVipService.setOrderNo(userPaidRecordVo.getOrderNo());
        userVipService.setUserId(userPaidRecordVo.getUserId());
        //3.1.3. 设置本次会员生效时间以及过期时间
        //判断当前用户是否是vip
        UserInfo userInfo = userInfoMapper.selectById(userPaidRecordVo.getUserId());
        Boolean isVip = false;
        Date now = new Date();
        if (userInfo.getIsVip().intValue() == 1
                && userInfo.getVipExpireTime().after(now)) {
            isVip = true;
        }
        //3.1.4 获取当前用户购买的套餐信息
        VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(userPaidRecordVo.getItemIdList().get(0));
        Integer serviceMonth = vipServiceConfig.getServiceMonth();

        if (isVip) {
            //说明当前用户是vip，获取当前用户会员过期时间+1作为本次会员的生效时间
            //会员生效时间+会员购买期限作为本次会员的过期时间
            //需要从userinfo中获取数据，不能从userVipService中获取，因为没有数据，为null
            Date vipExpireTime = userInfo.getVipExpireTime();
            DateTime startTime = DateUtil.offsetDay(vipExpireTime, 1);
            userVipService.setStartTime(startTime);
            userVipService.setExpireTime(DateUtil.offsetMonth(startTime, serviceMonth));

        } else {
            //说明当前用户不是vip，当前时间就是本次会员的生效时间，购买的期限就是会员的失效时间
            userVipService.setStartTime(now);
            DateTime expireTime = DateUtil.offsetMonth(now, serviceMonth);
            userVipService.setExpireTime(expireTime);
        }

        //3.1.4 保存会员购买记录
        userVipServiceMapper.insert(userVipService);

        //3.2 更新用户会员标识以及会员过期时间
        userInfo.setIsVip(1);
        userInfo.setVipExpireTime(userVipService.getExpireTime());
        userInfoMapper.updateById(userInfo);
    }
}
